﻿using Microsoft.VisualBasic;
using System;
using System.Security.Permissions;
using System.Security;
using System.Runtime.InteropServices;



namespace ContentManager
{
    class SAPModule
    {
        public ISAPLogonControl LogonControl;
        public ISAPLogonConnection R3Connection; 
        public ISAPFunctions Functions;
        public IFunction Func;

        public Boolean isconnected
        {
            get
            {
                Boolean ret = false;
                try
                {
                    if (R3Connection.IsConnected == CRfcConnectionStatus.tloRfcConnected)
                    {
                        ret = true;
                    }
                    else
                    {
                        ret = false;                        
                    }

                }
                catch
                {
                    ret = false;
                }
                return ret;
            }
        }

        [SecurityPermissionAttribute(SecurityAction.Assert,  Unrestricted  = true)]
        [System.Security.SecurityCriticalAttribute]
        public void R3Logon()
        {
            if (isconnected == true) return;            
                        
            LogonControl = (ISAPLogonControl)Interaction.CreateObject("SAP.LogonControl.1");           

            R3Connection = (ISAPLogonConnection)LogonControl.NewConnection();
            R3Connection.set_RfcWithDialog(1);            
            R3Connection.Logon(0, false);            

            Functions = (ISAPFunctions)Interaction.CreateObject("SAP.Functions");
            Functions.Connection = R3Connection;
        }


        public void Call_RSCMSLST(String Cat, String Rep, String LSTFILE)
        {
            Func = (IFunction)Functions.Add("SRM_RFC_SUBMIT_REPORT");
            IParameter ePROG = (IParameter)Func.get_Exports("REPORTNAME");
            IParameter RC = (IParameter)Func.get_Imports("RC");
            ISAPTaFacTables tables = (ISAPTaFacTables)Func.Tables;
            ISAPTaFacTable tRSPAR = (ISAPTaFacTable)tables.get_Item("RSPAR"); 
                                  
            ePROG.Value = "RSCMSLST";
            tRSPAR.AppendRow();
      
            TabSet_Value(tRSPAR, 1, 1, "CAT");                    
            TabSet_Value(tRSPAR, 1, 2, "P");
            TabSet_Value(tRSPAR, 1, 5, Cat);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 2, 1, "REP");
            TabSet_Value(tRSPAR, 2, 2, "P");
            TabSet_Value(tRSPAR, 2, 5, Rep);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 3, 1, "USE_FE");
            TabSet_Value(tRSPAR, 3, 2, "P");
            TabSet_Value(tRSPAR, 3, 5, "X");
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 4, 1, "LSTFILE");
            TabSet_Value(tRSPAR, 4, 2, "P");
            TabSet_Value(tRSPAR, 4, 5, LSTFILE);
            
            bool isCallSucess = false;
            isCallSucess = Func.Call();
            Functions.RemoveAll();

        }

        public System.Array ReadTable_first_Column(String Tabelle)
        {
            System.Array functionReturnValue = null;

            Func = (IFunction)Functions.Add("RFC_READ_TABLE");

            IParameter eQuery_Table = default(IParameter);
            IParameter eDelimiter = default(IParameter);
            ISAPTaFacTable TOptions = default(ISAPTaFacTable);
            ISAPTaFacTable TData = default(ISAPTaFacTable);
            ISAPTaFacTable TFields = default(ISAPTaFacTable);

            eQuery_Table = (IParameter)Func.get_Exports("QUERY_TABLE");
            eDelimiter = (IParameter)Func.get_Exports("DELIMITER");
            ISAPTaFacTables tables = (ISAPTaFacTables)Func.Tables;
            TOptions = (ISAPTaFacTable)tables.get_Item("OPTIONS"); 
            TData = (ISAPTaFacTable)tables.get_Item("DATA");
            TFields = (ISAPTaFacTable)tables.get_Item("FIELDS");

            eQuery_Table.Value = Tabelle;
            eDelimiter.Value = ";";

            bool isCallSucess = false;
            isCallSucess = Func.Call();


            if (isCallSucess == true && TData.RowCount != 0)
            {
                String[] TData2 = new String[TData.RowCount + 1];
                int n = 0;

                foreach (String TLine in (Array)TData.Data)
                {
                    TData2[n] = Strings.Split(TLine, ";")[0];
                    n++;
                }
                functionReturnValue = TData2;
            }
            else
            {
                functionReturnValue = null;
            }
            Functions.RemoveAll();
            return functionReturnValue;
        }


        public void Call_RSCMSEXL(String Cat, String Rep, String LSTFILE, String TRFILE)
        {

            Func = (IFunction)Functions.Add("SRM_RFC_SUBMIT_REPORT");
            IParameter ePROG = (IParameter)Func.get_Exports("REPORTNAME");
            ISAPTaFacTables tables = (ISAPTaFacTables)Func.Tables;
            ISAPTaFacTable tRSPAR = (ISAPTaFacTable)tables.get_Item("RSPAR");            

            ePROG.Value = "RSCMSEXL";
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 1, 1, "CAT");
            TabSet_Value(tRSPAR, 1, 2, "P");
            TabSet_Value(tRSPAR, 1, 5, Cat);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 2, 1, "REP");
            TabSet_Value(tRSPAR, 2, 2, "P");
            TabSet_Value(tRSPAR, 2, 5, Rep);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 3, 1, "USE_FE");
            TabSet_Value(tRSPAR, 3, 2, "P");
            TabSet_Value(tRSPAR, 3, 5, "X");
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 4, 1, "LSTFILE");
            TabSet_Value(tRSPAR, 4, 2, "P");
            TabSet_Value(tRSPAR, 4, 5, LSTFILE);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 5, 1, "TRFILE");
            TabSet_Value(tRSPAR, 5, 2, "P");
            TabSet_Value(tRSPAR, 5, 5, TRFILE);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 6, 1, "RFCDEST");
            TabSet_Value(tRSPAR, 6, 2, "P");
            TabSet_Value(tRSPAR, 6, 5, "SAPKPROTP_FRONTEND");


            bool isCallSucess = false;
            isCallSucess = Func.Call();
            Functions.RemoveAll();

        }


        public void Call_RSCMSIM(String CRep, String TRFILE)
        {

            Func = (IFunction)Functions.Add("SRM_RFC_SUBMIT_REPORT");
            IParameter ePROG = (IParameter)Func.get_Exports("REPORTNAME");
            IParameter RC = (IParameter)Func.get_Imports("RC");
            ISAPTaFacTables tables = (ISAPTaFacTables)Func.Tables;
            ISAPTaFacTable tRSPAR = (ISAPTaFacTable)tables.get_Item("RSPAR");
           
            ePROG.Value = "RSCMSIM";
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 1, 1, "CREP");
            TabSet_Value(tRSPAR, 1, 2, "P");
            TabSet_Value(tRSPAR, 1, 5, CRep);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 2, 1, "TRFILE");
            TabSet_Value(tRSPAR, 2, 2, "P");
            TabSet_Value(tRSPAR, 2, 5, TRFILE);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 3, 1, "RFCDEST");
            TabSet_Value(tRSPAR, 3, 2, "P");
            TabSet_Value(tRSPAR, 3, 5, "SAPKPROTP_FRONTEND");

            Boolean isCallSucess = false;
            isCallSucess = Func.Call();
            Functions.RemoveAll();
        }


        public void Call_DMS_KPRO_CHECK1(String P_Rep, String P_CDB, String P_CCS)
        {

            Func = Func = (IFunction)Functions.Add("SRM_RFC_SUBMIT_REPORT");
            IParameter ePROG = (IParameter)Func.get_Exports("REPORTNAME");
            IParameter RC = (IParameter)Func.get_Imports("RC");
            ISAPTaFacTables tables = (ISAPTaFacTables)Func.Tables;
            ISAPTaFacTable tRSPAR = (ISAPTaFacTable)tables.get_Item("RSPAR");            

            ePROG.Value = "DMS_KPRO_CHECK1";
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 1, 1, "P_REP");            
            TabSet_Value(tRSPAR, 1, 2, "P");
            TabSet_Value(tRSPAR, 1, 5, P_Rep);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 2, 1, "P_CDB");
            TabSet_Value(tRSPAR, 2, 2, "P");
            TabSet_Value(tRSPAR, 2, 5, P_CDB);
            tRSPAR.AppendRow();
            TabSet_Value(tRSPAR, 3, 1, "P_CCS");
            TabSet_Value(tRSPAR, 3, 2, "P");
            TabSet_Value(tRSPAR, 3, 5, P_CCS);

            bool isCallSucess = false;
            isCallSucess = Func.Call();
            Functions.RemoveAll();

        }

        private void TabSet_Value(ISAPTaFacTable table, Int32 row, Int32 column, Object value)
        {
            Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(table, null, "Value", new object[] { row, column, value }, null, null);
        }

        private void TabSet_Value(ISAPTaFacTable table, Int32 row, Object column, Object value)
        {
            table.Value(row, column, value);
        }
    }
}

